﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using MathNet.Numerics.Interpolation.Algorithms;
using paravili.Services;
using Sebarf.Core.Interfaces;
using Sebarf.Diagnostics.Interfaces;

#endregion

namespace paravili.Ui {
	/// <summary>
	/// Interaction logic for ActivityRatingVisulizerField.xaml
	/// </summary>
	public partial class ActivityRatingVisulizerField {
		#region Public Methods

		public ActivityRatingVisulizerField() {
			InitializeComponent();
			marker.Visibility = Visibility.Hidden;
		}

		public void Init(IServiceLocatorService serviceLocator) {
			serviceLocator.GetRegisteredService<IStatisticValuesLocatorService>().StatisticValueEntryAdded +=
				StatService_StatisticValueEntryAdded;
			serviceLocator.GetRegisteredService<IResetHandlerService>().Reset += OnReset;
		}

		#endregion

		#region Private Methods

		private void StatService_StatisticValueEntryAdded(object sender, StatisticValuesLocatorServiceEventArgs e) {
			if (e.Entry.Name == "Movement@ActivityRating") {
				m_rating = e.Entry;
				m_rating.PropertyChanged += OnValueChanged;
			}
			if (e.Entry.Name == "Movement@ClusterAreaCoverageRatio") {
				m_coverageRatio = e.Entry;
				m_coverageRatio.PropertyChanged += OnValueChanged;
			}
		}

		private void OnReset(object sender, EventArgs e) {
			marker.Visibility = Visibility.Hidden;
		}

		private void OnValueChanged(object sender, PropertyChangedEventArgs e) {
			if (m_rating != null && m_coverageRatio != null) {
				marker.Visibility = Visibility.Visible;

				var rating = (double)m_rating.Value;
				var coverageRatio = (double)m_coverageRatio.Value;

				//double xPos = Math.Max(0, ActualWidth * (rating - 1) / 4);
				double xPos = ActualWidth * rating;
				marker.SetValue(Canvas.LeftProperty, xPos);

				try {
					double yPos = ActualHeight - ActualHeight * coverageRatio * 7; // *15;
					if (double.IsNegativeInfinity(yPos) || double.IsPositiveInfinity(yPos))
						yPos = 0;
					marker.SetValue(Canvas.TopProperty, yPos);
				}
				catch (Exception ex) {
					Logger.WriteDebug(ex.Message);
					throw;
				}
			}
		}

		#endregion

		#region Private Fields

		private StatisticValueEntry m_coverageRatio;
		private StatisticValueEntry m_rating;

		#endregion
	}
}